package com.application.lucky.widget.popupwindow;

import android.app.ActionBar;
import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.application.lucky.R;
import com.application.lucky.bean.jsonBean.FriendBean;
import com.application.lucky.bean.jsonBean.FriendListbean;
import com.application.lucky.manager.ContactsManager;
import com.application.lucky.request.nav.UrlManager;
import com.application.mylibrary.utils.DateUtils;
import com.bigkoo.pickerview.builder.TimePickerBuilder;
import com.bigkoo.pickerview.listener.OnTimeSelectListener;
import com.bigkoo.pickerview.view.TimePickerView;
import com.bruce.pickerview.popwindow.DatePickerPopWin;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.schedulers.Schedulers;

public class BaoBiaoTimePop extends PopupWindow implements View.OnClickListener {

    private Context context;
    private TextView mTvAllTime;
    private RadioGroup mRgView;
    private TextView mTv1;
    private LinearLayout mLlStartTime;
    private TextView mTextStartTime;
    private View mLine;
    private LinearLayout mLlEndTime;
    private TextView mTextEndTime;
    private TimeSelectListener listener;
    private TimePickerView pickerView;
    private TimePickerView startPicker;

    public BaoBiaoTimePop(Context context, TimeSelectListener listener) {
        super(context);
        this.context = context;
        this.listener = listener;
        initView();
        initListener();
    }

    private void initListener() {

    }

    private long startTIme;
    private long endTime;

    private View conentView;


    private void initView() {
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        if (inflater != null) {
            conentView = inflater.inflate(R.layout.pop_baobiao_time, null);
        }
        if (conentView == null) {
            return;
        }
        mTvAllTime = (TextView) conentView.findViewById(R.id.tv_all_time);
        mTvAllTime.setOnClickListener(this);
        mRgView = (RadioGroup) conentView.findViewById(R.id.rg_view);
        mRgView.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                Date date = new Date();
                endTime = date.getTime();
                mTextEndTime.setText(DateUtils.getDateString(date));
                Calendar todayStart = Calendar.getInstance();


                todayStart.setTime(date);
                todayStart.set(Calendar.HOUR_OF_DAY, 0);
                todayStart.set(Calendar.MINUTE, 0);
                todayStart.set(Calendar.SECOND, 0);
                todayStart.set(Calendar.MILLISECOND, 0);
                switch (i) {
                    case 1:
                        break;
                    case 2:
                        todayStart.add(Calendar.DAY_OF_YEAR, -7);
                        break;
                    case 3:
                        todayStart.add(Calendar.DAY_OF_YEAR, -30);
                        break;
                    case 4:
                        todayStart.add(Calendar.DAY_OF_YEAR, -90);
                        break;
                    case 5:
                        todayStart.add(Calendar.DAY_OF_YEAR, -180);
                        break;
                }
                startTIme = todayStart.getTime().getTime();
                mTextStartTime.setText(DateUtils.getDateString(todayStart.getTime()));

            }
        });
        conentView.findViewById(R.id.ll).

                setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {

                    }
                });
        conentView.findViewById(R.id.tv_comint).

                setOnClickListener(this);

        mTv1 = (TextView) conentView.findViewById(R.id.tv1);
        mLlStartTime = (LinearLayout) conentView.findViewById(R.id.ll_start_time);
        mTextStartTime = (TextView) conentView.findViewById(R.id.text_start_time);
        mLine = (View) conentView.findViewById(R.id.line);
        mLlEndTime = (LinearLayout) conentView.findViewById(R.id.ll_end_time);
        mTextEndTime = (TextView) conentView.findViewById(R.id.text_end_time);
        mLlStartTime.setOnClickListener(this);
        mLlEndTime.setOnClickListener(this);
        this.

                setWidth(ActionBar.LayoutParams.MATCH_PARENT);
        this.

                setHeight(ActionBar.LayoutParams.WRAP_CONTENT);
        this.

                setFocusable(true);
        this.

                setBackgroundDrawable(new BitmapDrawable());
//        this.setAnimationStyle(R.style.popwin_add_product_style);
        this.

                setOutsideTouchable(true);

        setContentView(conentView);

//        setAnimationStyle(R.style.tagsAnim);


    }


    public void show(View v) {
        try {
//                showAtLocation(v, Gravity.TOP, 0, 0);
            showAsDropDown(v, 0, 0);
//                showAtLocation(v, Gravity.TOP, 0, 0);
        } catch (Exception e) {

        }


    }


    @Override
    public void onClick(View view) {
        Calendar selectedDate = Calendar.getInstance();
        Calendar startDate = Calendar.getInstance();
        //startDate.set(2013,1,1);
        Calendar endDate = Calendar.getInstance();
        //endDate.set(2020,1,1);

        //正确设置方式 原因：注意事项有说明
        startDate.set(2018, 1, 1);

        switch (view.getId()) {
            case R.id.ll_end_time:

//                if (pickerView == null) {
//                    pickerView = new TimePickerBuilder(context, new OnTimeSelectListener() {
//                        @Override
//                        public void onTimeSelect(Date date, View v) {//选中事件回调
//                            mRgView.clearCheck();
//                            endTime = date.getTime();
//
//                            mTextEndTime.setText(DateUtils.getDateString(date));
//                        }
//                    }).setType(new boolean[]{true, true, true, true, true, true})// 默认全部显示
//                            .setCancelText("取消")//取消按钮文字
//                            .setSubmitText("确定")//确认按钮文字
//                            .setTitleSize(20)//标题文字大小
//                            .setTitleText("选择时间")//标题文字
//                            .setOutSideCancelable(true)//点击屏幕，点在控件外部范围时，是否取消显示
//                            .isCyclic(true)//是否循环滚动
//                            .setTitleColor(Color.BLACK)//标题文字颜色
//                            .setSubmitColor(Color.parseColor("#DE3F35"))//确定按钮文字颜色
//                            .setCancelColor(Color.parseColor("#ffd8d8d8"))//取消按钮文字颜色
//                            .setTitleBgColor(Color.WHITE)//标题背景颜色 Night mode
//                            .setBgColor(Color.parseColor("#ffd8d8d8"))//滚轮背景颜色 Night mode
//                            .setDate(selectedDate)// 如果不设置的话，默认是系统时间*/
//                            .setRangDate(startDate, endDate)//起始终止年月日设定
//                            .setLabel("年", "月", "日", "时", "分", "秒")//默认设置为年月日时分秒
//                            .isCenterLabel(false) //是否只显示中间选中项的label文字，false则每项item全部都带有label。
//                            .isDialog(false)//是否显示为对话框样式
//                            .build();
//                }
//                pickerView.show(view,true);
                break;
            case R.id.ll_start_time:

//                if (startPicker == null) {
//                    mRgView.clearCheck();
//                    startPicker = new TimePickerBuilder(context, new OnTimeSelectListener() {
//                        @Override
//                        public void onTimeSelect(Date date, View v) {//选中事件回调
//                            startTIme = date.getTime();
//                            mTextStartTime.setText(DateUtils.getDateString(date));
//                        }
//                    }).setType(new boolean[]{true, true, true, true, true, true})// 默认全部显示
//                            .setCancelText("取消")//取消按钮文字
//                            .setSubmitText("确定")//确认按钮文字
//                            .setTitleSize(20)//标题文字大小
//                            .setTitleText("选择时间")//标题文字
//                            .setOutSideCancelable(true)//点击屏幕，点在控件外部范围时，是否取消显示
//                            .isCyclic(true)//是否循环滚动
//                            .setTitleColor(Color.BLACK)//标题文字颜色
//                            .setSubmitColor(Color.parseColor("#DE3F35"))//确定按钮文字颜色
//                            .setCancelColor(Color.parseColor("#ffd8d8d8"))//取消按钮文字颜色
//                            .setTitleBgColor(Color.WHITE)//标题背景颜色 Night mode
//                            .setBgColor(Color.parseColor("#ffd8d8d8"))//滚轮背景颜色 Night mode
//                            .setDate(selectedDate)// 如果不设置的话，默认是系统时间*/
//                            .setRangDate(startDate, endDate)//起始终止年月日设定
//                            .setLabel("年", "月", "日", "时", "分", "秒")//默认设置为年月日时分秒
//                            .isCenterLabel(false) //是否只显示中间选中项的label文字，false则每项item全部都带有label。
//                            .isDialog(false)//是否显示为对话框样式
//                            .build();
//                }
//                startPicker.show(view,true);
                break;
            case R.id.tv_all_time:
                startTIme = new Date().getTime();
                endTime = 1;
                if (listener != null) {
                    listener.selectTime(startTIme, endTime);
                    dismiss();
                }
                break;
            case R.id.tv_comint:
                if (listener != null) {
                    listener.selectTime(startTIme, endTime);
                }
                dismiss();

        }
    }


    public interface TimeSelectListener {
        void selectTime(long time, long endTime);


    }
}
